package org.tong.activiti6.test.utils;

import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.activiti.engine.*;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.Execution;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author
 * @date 2022-08-10 16:13
 */
@Slf4j
@Service
@Getter
public class ActivitUtils {

    @Autowired
    private RepositoryService repositoryService;

    @Autowired
    private RuntimeService runtimeService;

    @Autowired
    private TaskService taskService;

    @Autowired
    private ManagementService managementService;

    @Autowired
    private ProcessEngine processEngine;

    public String deploy(String bpmnPath) {
        Deployment deploy = repositoryService.createDeployment()
                .addClasspathResource(bpmnPath)
                .deploy();
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().deploymentId(deploy.getId()).singleResult();
        System.out.println("部署了BPMN文件：procDefId=" + processDefinition.getId() + ",   bpmnPath=" + bpmnPath);
        return processDefinition.getId();
    }

    public String deploy(String bpmnPath, String tenantId) {
        Deployment deploy = repositoryService.createDeployment()
                .addClasspathResource(bpmnPath)
                .tenantId(tenantId)
                .deploy();
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().deploymentId(deploy.getId()).singleResult();
        System.out.println("部署了BPMN文件：procDefId=" + processDefinition.getId() + ",   bpmnPath=" + bpmnPath);
        return processDefinition.getId();
    }


    public String startInst(String procDefId, Map<String, Object> varMap) {
        ProcessInstance processInstance = runtimeService.startProcessInstanceById(procDefId, varMap);
        return processInstance.getId();
    }

    public String startInst(String procDefId, String tenantId, Map<String, Object> varMap) {
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().processDefinitionId(procDefId).singleResult();
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKeyAndTenantId(processDefinition.getKey(), varMap, tenantId);
        return processInstance.getId();
    }

    public String startInst(String procDefId) {
        ProcessInstance processInstance = runtimeService.startProcessInstanceById(procDefId);
        System.out.println("启动了流程实例:" + processInstance.getId());
        return processInstance.getId();
    }

    public void complete(String taskId) {
        taskService.complete(taskId);
    }

    public void complete(String taskId, Map<String, Object> variables) {
        taskService.complete(taskId, variables);
    }

    public List<Task> listTasks() {
        return taskService.createTaskQuery().list();
    }

    public List<ProcessInstance> listInst() {
        return runtimeService.createProcessInstanceQuery().list();
    }

    public List<Execution> listExecute() {
        return runtimeService.createExecutionQuery().list();
    }

    public void showAllTask() {
        taskService.createTaskQuery().list().stream().map(task -> {
            return String.join(" ", "taskId=" + task.getId(), "taskName=" + task.getName(), "key=" + task.getTaskDefinitionKey());
        }).forEach(System.out::println);
    }

    public void deleteAll() throws IOException {
        repositoryService.createDeploymentQuery().list().stream().map(Deployment::getId).forEach(deployId -> {
            repositoryService.deleteDeployment(deployId, true);
        });
        System.out.println("已删除所有数据");
    }

    public void deleteInst(String instId, String reason) {
        runtimeService.deleteProcessInstance(instId, reason);
    }
}
